package Limit.Game.MarkSix;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

public class MarkSix_Data_Table 
{
	private int _ID;
	private int _totalWager;
	private int _totalReward;
	private int _Type1;
	private int _Type2;
	private int _Type3;
	private int _Type4;
	private int _Type6;
	private int _Type7;
	private int _Type8;
	private int _Type9;
	private int _Type10;
	
	public int getMarkId(){return _ID;}
	public void setMarkId(int i){_ID = i;}
	
	public int getTotalWager(){return _totalWager;}
	public void setTotalWager(int i){_totalWager = i;}
	
	public int getTotalReward(){return _totalReward;}
	public void setTotalReward(int i){_totalReward = i;}
	
	public int getType1(){return _Type1;}
	public void setType1(int i){_Type1 = i;}
	
	public int getType2(){return _Type2;}
	public void setType2(int i){_Type2 = i;}
	
	public int getType3(){return _Type3;}
	public void setType3(int i){_Type3 = i;}
	
	public int getType4(){return _Type4;}
	public void setType4(int i){_Type4 = i;}
	
	public int getType6(){return _Type6;}
	public void setType6(int i){_Type6 = i;}
	
	public int getType7(){return _Type7;}
	public void setType7(int i){_Type7 = i;}
	
	public int getType8(){return _Type8;}
	public void setType8(int i){_Type8 = i;}
	
	public int getType9(){return _Type9;}
	public void setType9(int i){_Type9 = i;}
	
	public int getType10(){return _Type10;}
	public void setType10(int i){_Type10 = i;}
	
	//每天第一次運行，先生成當天空的統計資料
	public void insertTodayMarkSixTotal(){
		int phase = this.loadMarkSixPhase(); 
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("INSERT INTO game_marksix_total SET datetime=SYSDATE(), Phase=?");
			pstm.setInt(1, phase);
			pstm.execute();
		}
		catch (Exception e){System.out.print(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}				
	}
	
	//玩家確認購買的同時，更新統計表彩票類型
	public void updateMarkSixTotalTypeNum(int[] type){
		int id = this.loadMarkSixID();
		Connection con = null;
		PreparedStatement pstm = null;
		try 
		{  
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE game_marksix_total SET Type1=Type1+? ,Type2=Type2+? ,Type3=Type3+? ,Type4=Type4+? ,Type6=Type6+? ,Type7=Type7+? ,Type8=Type8+? ,Type9=Type9+? ,Type10=Type10+? Where id=?");
			int i=1;
			pstm.setInt(i++, type[1]);
			pstm.setInt(i++, type[2]);
			pstm.setInt(i++, type[3]);
			pstm.setInt(i++, type[4]);
			pstm.setInt(i++, type[6]);
			pstm.setInt(i++, type[7]);
			pstm.setInt(i++, type[8]);
			pstm.setInt(i++, type[9]);
			pstm.setInt(i++, type[10]);
			pstm.setInt(i++, id);
			pstm.execute();
		}
		catch (Exception e){System.out.print(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}
	}
	public void updateMarkSixTotalWager(int wager)
	{
		int id = this.loadMarkSixID();
		Connection con = null;
		PreparedStatement pstm = null;
		try 
		{  
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE game_marksix_total SET TotalWager = TotalWager + "+wager+" Where ID = ?");
			pstm.setInt(1, id);
			pstm.execute();
		}
		catch (Exception e){System.out.print(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}
	}
	
	//將當天計算開出的七個中獎號碼更新到統計資料表
	public void updateMarkSixTotalNum(String[] sevenNum)
	{
		int id = this.loadMarkSixID();
		Connection con = null;
		PreparedStatement pstm = null;
		try 
		{  
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE game_marksix_total SET num1=? ,num2=? ,num3=? ,num4=? ,num5=? ,num6=? ,num7=? Where ID=?");
			pstm.setString(1, sevenNum[0]);
			pstm.setString(2, sevenNum[1]);
			pstm.setString(3, sevenNum[2]);
			pstm.setString(4, sevenNum[3]);
			pstm.setString(5, sevenNum[4]);
			pstm.setString(6, sevenNum[5]);
			pstm.setString(7, sevenNum[6]);
			pstm.setInt(8, id);
			pstm.execute();
		}
		catch (Exception e){System.out.print(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}
	}
	
	//將當天中獎者的獎金額更新到統計資料表
	public void updateMarkSixTotalReward(int totalReward){
		int id = this.loadMarkSixID();
		Connection con = null;
		PreparedStatement pstm = null;
		try 
		{  
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE game_marksix_total SET totalReward=? Where ID = ?");
			pstm.setInt(1, totalReward);
			pstm.setInt(2, id);
			pstm.execute();
		}
		catch (Exception e){System.out.print(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}		
	}
	
	
	public void saveMarkSixData(int income)
	{	
		int id = this.loadMarkSixID();
		Connection con = null;
		PreparedStatement pstm = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE game_marksix_total SET Income=? Where ID = ?");   
			pstm.setInt(1, income);
			pstm.setInt(2, id);
			pstm.execute();

		}
		catch (Exception e) 
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}		
	}
	
	public int loadMarkSixID()
	{
		int i = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{        
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT ID FROM game_marksix_total Order By ID DESC limit 0, 1");
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				i = rs.getInt("ID");
			}
		}
		
		catch (Exception e)	{System.out.print(e.getMessage());}
		finally {SQLUtil.close(rs, pstm, con);}
		return i;	
	}
	
	public String[] loadMarkSixInfo()
	{
		String[] nums = new String[7];
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{        
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM game_marksix_total Where ID = "+(loadMarkSixID()-1));
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				nums[0] = rs.getString("num1");
				nums[1] = rs.getString("num2");
				nums[2] = rs.getString("num3");
				nums[3] = rs.getString("num4");
				nums[4] = rs.getString("num5");
				nums[5] = rs.getString("num6");
				nums[6] = rs.getString("num7");
			}
		}
		
		catch (Exception e)	{System.out.print(e.getMessage());}
		finally {SQLUtil.close(rs, pstm, con);}
		return nums;	
	}
	
	public int loadMarkSixPhase()
	{
		int i = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{        
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT Phase FROM game_marksix_total Order By ID DESC limit 0, 1");
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				i = rs.getInt("Phase");
			}
		}
		
		catch (Exception e)	{System.out.print(e.getMessage());}
		finally {SQLUtil.close(rs, pstm, con);}
		return i+1;	
	}
	
	public int loadMarkSixIncome()
	{
		int j = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT Income FROM game_marksix_total Order By ID DESC limit 0, 1");
			rs = pstm.executeQuery();
				
			while (rs.next())
			{
				j = rs.getInt("Income");
			}
		}
		
		catch (Exception e)
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(rs, pstm, con);
		}
		return j;	
	}
	
	public String[] loadMarkSixNum()
	{
		String[] num = new String[7];
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM game_marksix_total Order By ID DESC limit 0, 1");
			rs = pstm.executeQuery();
				
			while (rs.next())
			{
				num[0] = rs.getString("num1");
				num[1] = rs.getString("num2");
				num[2] = rs.getString("num3");
				num[3] = rs.getString("num4");
				num[4] = rs.getString("num5");
				num[5] = rs.getString("num6");
				num[6] = rs.getString("num7");
			}
		}
		
		catch (Exception e)
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(rs, pstm, con);
		}
		return num;
	}
}
